'''
Created on Nov 25, 2010

@author: philip
'''
from django.shortcuts import render_to_response
from tips.tip_service import TipService
from django.template.loader import render_to_string
from links.link_service import LinkService
from events.event_service import EventService
from photos.photo_service import GetNotificationsRequest, PhotoService, GetPhotosRequest
from places.place_service import PlaceService, GetPlacesRequest
from community_service import CommunityService
from django.http import Http404, HttpResponse
from tips.models import Tip
from links.models import Link
from search.core import search as index_search
from events.models import Event
from places.models import Place
from photos.models import Photo



def index(request,communityId):
    community = CommunityService().getCommunity(communityId)
    
    if community is None: raise Http404
    
    tips = TipService().getTips4Community(community.urlName)
    links = LinkService().getLinks4Community(community.urlName)
    events = EventService().getUpcomingEvents()
    photoNotificationGroups = PhotoService().getNotifications(GetNotificationsRequest(community.urlName))
    photoPreviews = PhotoService().getPhotos(GetPhotosRequest(community.urlName, 6))
    placesPreviews = PlaceService().getPlaces(GetPlacesRequest(community.urlName, 5)) 
    
    content = []
    
    for event in events:
        content.append(render_to_string("events/events_renderer.html",{"event":event}))
    
    for pnotification in photoNotificationGroups:
        content.append(render_to_string("notifications/notifications_renderer.html", {"notificationGroup":pnotification}))
    
    for tip in tips:
        content.append(render_to_string("tips/tips_renderer.html",{"tip":tip}))
    
    for link in links:
        content.append(render_to_string("links/links_renderer.html",{"link":link}))
    
    
    return render_to_response("communities/community.html",
                              {"community":community, 
                               "photoPreviews":photoPreviews,
                               "placesPreviews":placesPreviews,
                               "content":content})
    
def search(request):
    q = request.REQUEST["q"]
    
    #events
    es = index_search(Event, q, language="fr")
    
    #tips
    ts = index_search(Tip, q, language="fr")
    
    #links
    ls = index_search(Link,q, language="fr")
    
    #places
    ps = index_search(Place,q, language="fr")
    
    #photos
    photos = index_search(Photo, q, language="fr")
    
    content = []
    
    for event in es:
        content.append(render_to_string("events/events_renderer.html",{"event":event}))
     
    for tip in ts:
        content.append(render_to_string("tips/tips_renderer.html",{"tip":tip}))
    
    for link in ls:
        content.append(render_to_string("links/links_renderer.html",{"link":link}))
    
    for place in ps:
        content.append(render_to_string("places/places_renderer.html",{"place":place}))
    
    for photo in photos: 
        content.append(render_to_string("photos/photos_renderer.html",{"photo":photo}))
    
    return render_to_response("search/search_results.html",{"content":content})